package cn.kgc.controller;

import cn.kgc.entity.PmsBrand;
import cn.kgc.entity.PmsCategory;
import cn.kgc.service.PmsBrandService;
import cn.kgc.service.PmsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.kgc.entity.PmsCategoryBrandRelation;
import cn.kgc.dto.PmsCategoryBrandRelationAddDTO;
import cn.kgc.service.PmsCategoryBrandRelationService;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 品牌分类关联(PmsCategoryBrandRelation)表控制层
 *
 * @author makejava
 * @since 2022-11-29 12:07:41
 */
@RestController
@RequestMapping("pms/categoryBrandRelation")
@Api(tags = "品牌分类关联表")
@Validated
@Slf4j
public class PmsCategoryBrandRelationController {
    /**
     * 服务对象
     */
    @Resource
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;

    @Resource
    private PmsCategoryService pmsCategoryService;
    @Resource
    private PmsBrandService pmsBrandService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "brandId", value = "品牌id", paramType = "query", dataType = "int")
    })
    @CrossOrigin
    public IPage<PmsCategoryBrandRelation> selectAll(@RequestParam(defaultValue = "1") int current,
                                                     @RequestParam(defaultValue = "5") int pageSize,
                                                     @RequestParam Integer brandId) {
        log.info("查询{},对应分类的信息:",brandId);
        LambdaQueryWrapper<PmsCategoryBrandRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsCategoryBrandRelation::getBrandId,brandId);
        IPage<PmsCategoryBrandRelation> page = new Page(current, pageSize);
        return pmsCategoryBrandRelationService.page(page,lambdaQueryWrapper);
    }


    /**
     * 新增数据
     *
     * @param pmsCategoryBrandRelationDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody PmsCategoryBrandRelationAddDTO pmsCategoryBrandRelationDTO) {
        // 新增保证数据库中分类与品牌的id唯一  方案一  可以在数据库中添加唯一的组合索引

        // 方案二  数据库中根据分类的id和品牌的id查询关系是否存在，如果存在，返回错误信息
        Long brandId=pmsCategoryBrandRelationDTO.getBrandId();
        Long catelogId=pmsCategoryBrandRelationDTO.getCatelogId();
        PmsBrand pmsBrand=pmsBrandService.getById(brandId);
        PmsCategory pmsCategory=pmsCategoryService.getById(catelogId);

        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        BeanUtils.copyProperties(pmsCategoryBrandRelationDTO, pmsCategoryBrandRelation);
        pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
        pmsCategoryBrandRelation.setCatelogName(pmsCategory.getName());
        return this.pmsCategoryBrandRelationService.save(pmsCategoryBrandRelation);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.pmsCategoryBrandRelationService.removeById(id);
    }
}
